package com.lagou.rpc;

import com.lagou.configBean.ZkClient;
import org.apache.curator.framework.CuratorFramework;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.data.Stat;

import java.util.List;

/**
 * @author ascetic
 * @version 1.0
 * @ClassName RegisterService
 * @Description TODO
 * @date 2020-07-03 17:57
 */
public class RegisterService {

    public static String service;
    public static String serviceAckTime;

    public static  void reg(String serverName,String ip,String port) throws Exception {

        CuratorFramework client = ZkClient.getZKClient();
        Stat stat = client.checkExists().forPath(serverName);
        if(null == stat)
            client.create().withMode(CreateMode.PERSISTENT).forPath(serverName);


        service = serverName+"/"+ip + ":"+port;
        serviceAckTime = service + ":" + "lastAckTime";
        if(null == client.checkExists().forPath(service))
            client.create().withMode(CreateMode.EPHEMERAL).forPath(service);
        if(null == client.checkExists().forPath(serviceAckTime)){
            client.create().withMode(CreateMode.EPHEMERAL).forPath(serviceAckTime);
            client.setData().forPath(serviceAckTime,
                    Long.valueOf(System.currentTimeMillis()).toString().getBytes());
        }else {
            client.setData().forPath(RegisterService.serviceAckTime,
                    Long.valueOf(System.currentTimeMillis()).toString().getBytes());
        }


    }

}